package com.siemens.industrial.web.mvc.controller.partner.user;

import com.siemens.industrial.model.partner.Metadata;
import com.siemens.industrial.utils.Definition;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.JsonToOtherFormat;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerDeviceService;
import com.siemens.industrial.web.mvc.service.partner.PartnerMetadataService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2017/2/12.
 */
@Controller
public class PartnerStatusTypeController {
    @Resource
    private PartnerMetadataService partnerMetadataService;

    @Resource
    private PartnerDeviceService partnerDeviceService;

    @Resource
    private TenantService tenantService;

    @RequestMapping(value = "/partner/{tenant}/tenant/statusType/info",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getStatusTypeInfo(@PathVariable("tenant") String tenantKey){
        List list = Definition.STATUSTYPE.getData();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }

    @RequestMapping(value = "/partner/{tenant}/tenant/statusType/infoById",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getMyStatusInfo(@PathVariable("tenant") String tenantKey){
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Metadata m = partnerMetadataService.getStatus(tenantId,"statustype");
        if (m==null){
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        }else {
            List<Integer> list = JsonToOtherFormat.buildStringToList(m.getMetaDefinition());
            List<Map<String,Object>> mapList = new ArrayList<>();
            for (int a : list) {
                mapList.add(Definition.STATUSTYPE.getData(a));
            }
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(mapList).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/tenant/statusType/moreInfo",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getInfo(@PathVariable("tenant") String tenantKey){
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<String> types = partnerDeviceService.getDeviceType(tenantId);
        List list = Definition.STATUSTYPE.getData();
        return RestResponse.create(GlobalResponseCode.SUCCESS).put("types",types).put("status",list).build();
    }
}
